package com.aaa.qy127.dao.impl;

import com.aaa.qy127.dao.UserDao;
import com.aaa.qy127.entity.User;
import com.aaa.qy127.util.JDBCUtil;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Repository("UserDaoImpl")
public class UserDaoImpl implements UserDao {
    /**
     * 获取用户信息
     * @param user
     * @return
     */
    @Override
    public List<Map<String, Object>> getUser(User user) {
        StringBuilder sql = new StringBuilder("SELECT userid,phone,password,name,cardID,address,status from t_user where 1=1");
        if (!"".equals(user.getPhone()) && user.getPhone()!=null){
            sql.append(" and phone like '%").append(user.getPhone()).append("%'");
        }
        sql.append(" limit ?,?");
        List<Map<String, Object>> maps = JDBCUtil.queryMap(sql.toString(),(user.getPage()-1)*user.getLimit(),user.getLimit());
        if(maps.size()>0){
            return maps;
        }
        return null;
    }
    /**
     * 获取用户信息条数
     * @param user
     * @return
     */
    @Override
    public int getUserCount(User user) {
        StringBuilder sql = new StringBuilder("SELECT count(userid) from t_user where 1=1");
        if (!"".equals(user.getPhone()) && user.getPhone()!=null){
            sql.append(" and phone like '%").append(user.getPhone()).append("%'");
        }
        List<List> lists = JDBCUtil.queryList(sql.toString());
        if(lists.size()>0){
            Object o = lists.get(0).get(0);
            int i = Integer.parseInt(o + "");
            return i;
        }
        return 0;
    }
    /**
     * 获取用户禁用信息
     * @param user
     * @return
     */
    @Override
    public List<Map<String, Object>> getcause(User user) {
        String sql = "select id,date,userid,cause from t_user_jy where userid = ?";
        List<Map<String, Object>> maps = JDBCUtil.queryMap(sql,user.getUserid());
        if(maps.size()>0){
            return maps;
        }
        return null;
    }
    /**
     * 修改禁用状态
     * @param user
     * @return
     */
    @Override
    public int statusno(User user) {
        String sql = "update t_user set status = 1 where userid = ?";
        int update = JDBCUtil.update(sql, user.getUserid());
        return update;
    }
    /**
     * 修改使用状态
     * @param user
     * @return
     */
    @Override
    public int statusyes(User user) {
        String sql = "update t_user set status = 0 where userid = ?";
        int update = JDBCUtil.update(sql, user.getUserid());
        return update;
    }
    /**
     * 添加禁用原因
     * @param user
     * @return
     */
    @Override
    public int addcause(User user) {
        String sql = "insert into t_user_jy values(null,?,?,?)";
        int update = JDBCUtil.update(sql, new Date(),user.getUserid(), user.getCause());
        return update;
    }
}
